package com.chaopei.dao.impl;

import com.chaopei.dao.StuCourSeleDao;
import com.chaopei.entity.StuCourSeleView;
import com.chaopei.utils.MyTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Corday 天涯
 * @Date 2022/3/24 14:48
 */
public class StuCourSeleDaoImpl implements StuCourSeleDao {
    @Override
    public List<StuCourSeleView> selectAllStuCourSeles() {
        Connection connection = MyTools.getConn();
        PreparedStatement ps = null;
        List<StuCourSeleView> list = null;
        try {
            //项目中不要用*号
            //请完成行转列显示,学号,姓名,年龄,英语,高数,计算机
            //                 8001 李明  22   98   89    92
            //                 8002  小翠  21   97  92    95
            String sql = "select * from stu_cour_sel_v";
            list = new ArrayList<>();
            ps = connection.prepareStatement(sql);
            //此结果集对象中以匿名的方式保存所有查询的记录
            //每一条记录对象一个对象
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                StuCourSeleView scsv = new StuCourSeleView();
                scsv.setName(rs.getString("name"));
                scsv.setBirth(rs.getDate("birth"));
                scsv.setCname(rs.getString("cname"));
                scsv.setScore(rs.getDouble("score"));
                list.add(scsv);
            }
//            关闭连接
            MyTools.closeConn(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
